package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.entity.pojo.HumEntity;
import com.hgxd.result.Result;
import com.hgxd.service.HumService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: wtf
 * @Description: 温度传感器表
 * @Date: 2022/10/20/11:21
 */
@Api(tags = "温度传感器表")
@CrossOrigin
@RestController
@RequestMapping("/api/hum")
public class HumController {

    @Resource(name = "HumService")
    private HumService humService;

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result<Boolean> insert(@RequestBody HumEntity humEntity) {
        humEntity.setCreatTime(new Date());
        humEntity.setUpdateTime(new Date());
        return Result.ok(humService.save(humEntity));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody HumEntity humEntity) {
        humEntity.setUpdateTime(new Date());
        return Result.ok(humService.updateById(humEntity));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Integer id) {
        return Result.ok(humService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(humService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    public Result<List<HumEntity>> findAll() {
        return Result.ok(humService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    public Result<HumEntity> findOne(@PathVariable Integer id){
        return Result.ok(humService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/get/page")
    public Result<Page<HumEntity>> findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<HumEntity> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(humService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }
}
